﻿using blackhouse.WebNemo.Business.Groups;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace blackhouse.WebNemo.Business.Extensionable {
    public class Extensions {

        #region Fields

        private IExtensionManager manager;

        #endregion

        #region Constructors

        public Extensions(IGroup group) {
            this.manager = Factory.Instance().GetExtensionManager(group);
        }

        #endregion

        #region Methods

        public GroupExtension GetGroupExtension() {
            Type t = this.GetGroupExtensionType();
            if (t == null)
                return GroupExtension.NULL;

            GroupExtension ext = Activator.CreateInstance(t) as GroupExtension;
            if (ext == null)
                return GroupExtension.NULL;

            return ext;
        }

        public Type GetGroupExtensionType() {
            return this.GetExtensionType(this.manager.GroupExtensionTypeDeclaration);
        }

        public RecordExtension GetGroupRecordExtension() {
            return this.GetGroupRecordExtension(null);
        }

        public RecordExtension GetGroupRecordExtension(GroupRecord record) {
            Type t = this.GetGroupRecordExtensionType();
            if (t == null)
                return RecordExtension.NULL;

            RecordExtension ext = Activator.CreateInstance(t, record) as RecordExtension;
            if (ext == null)
                return RecordExtension.NULL;

            return ext;
        }

        public Type GetGroupRecordExtensionType() {
            return this.GetExtensionType(this.manager.GroupRecordExtensionTypeDeclaration);
        }

        private Type GetExtensionType(string extensionName) {
            if (string.IsNullOrEmpty(extensionName))
                return null;
            Type t = Type.GetType(extensionName, false, false);
            return t;
        }

        public void ChangeGroupExtension(Type groupExtensionType) {
            if (groupExtensionType == null) {
                if (this.GetGroupExtensionType() != null)
                    this.manager.RemoveGroupExtension();
            }
            else {
                if (!(typeof(GroupExtension).IsAssignableFrom(groupExtensionType)))
                    throw new ArgumentException(groupExtensionType.AssemblyQualifiedName + " nie jest poprawnym typem dla rozszerzenia grupy");
                if (!groupExtensionType.Equals(this.GetGroupExtensionType()))
                    this.manager.SetGroupExtension(groupExtensionType);
            }
        }

        public void ChangeGroupRecordExtension(Type groupRecordExtensionType) {
            if (groupRecordExtensionType == null) {
                if (this.GetGroupRecordExtensionType() != null)
                    this.manager.RemoveGroupRecordExtension();
            }
            else {
                if (!(typeof(RecordExtension).IsAssignableFrom(groupRecordExtensionType)))
                    throw new ArgumentException(groupRecordExtensionType.AssemblyQualifiedName + " nie jest poprawnym typem dla rozszerzenia rekordu grupy");
                if (!groupRecordExtensionType.Equals(this.GetGroupRecordExtensionType()))
                    this.manager.SetGroupRecordExtension(groupRecordExtensionType);
            }
        }

        #endregion

    }
}
